const dataConst = require('./app/constant/data-const');
const fileSrv = require('./app/utility/fileSrv');

module.exports = app => {
    app.beforeStart(async () => {
        console.log(`==== egg env: ${app.config.env} ====`);
        console.log(`==== node env: ${process.env.NODE_ENV} ====`);

        if (process.env.NODE_ENV === 'development') {
            dataConst.setServerUrl('http://localhost:9220');
            app.resDir = `${app.config.HOME}/eden-resource`;
        } else {
            dataConst.setServerUrl('https://server.castjp.top');
            app.resDir = '/home/eden-resource';
        }
        console.log('app resDir:', app.resDir);

        app.once('server', server => {
            fileSrv.createDir(`${app.resDir}`);
            fileSrv.createDir(`${app.resDir}/temp`);
            fileSrv.createDir(`${app.resDir}/meeting`);
            fileSrv.createDir(`${app.resDir}/member`);
        });

        app.on('error', (err, ctx) => {
            console.log('app error:', err);
        });

        app.on('request', ctx => {
            // console.log('app request');
        });

        app.on('response', ctx => {
            const {status, message} = ctx.response;
            // console.log('app response status:', status, 'message:', message);
        });
    });
};